/*
 * util.c
 *
 *  Created on: 2009-7-30
 *      Author: wyb
 */

#include "util.h"

void output_double_array(struct double_array *value, char *str) {
	printf("--------------------\n");
	printf("%s\n", str);
	printf("--------------------\n");
	if (value == NULL) {
		printf("NULL");
		return;
	}
	int i;
	for (i = 0; i < value->N; ++i) {
		printf("%.4f \n", value->data[i]);
	}
	printf("--------------------\n");
}

void file_double_array(struct double_array *value, char *str) {
	FILE *fp;
	if ((fp = fopen(str, "w+")) == NULL) {
		printf("can't open %s", str);
	}

	int i;
	for (i = 0; i < value->N - 1; i++) {
		fprintf(fp, "%f,", value->data[i]);
	}
	fprintf(fp, "%f", value->data[value->N - 1]);
	fclose(fp);
}

struct double_array *merge_double_array(struct double_array *a1,
		struct double_array *a2) {
	struct double_array *result;
	result = malloc(sizeof(struct double_array));
	result->N = a1->N + a2->N;
	result->data = malloc(result->N * sizeof(double));

	int i;
	for (i = 0; i < a1->N; i++) {
		result->data[i] = a1->data[i];
	}

	for (i = 0; i < a2->N; i++) {
		result->data[i + a1->N] = a2->data[i];
	}
	return result;
}
